/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import pojo.ChiTietChuyenBayPOJO;

/**
 *
 * @author b2
 */
public class ChiTietChuyenBayDAO {

    public static ArrayList<ChiTietChuyenBayPOJO> LayDanhSachChiTietCB() {

        ArrayList<ChiTietChuyenBayPOJO> ds = new ArrayList<ChiTietChuyenBayPOJO>();
        try {
            String sql = "SELECT * FROM CHITIETCHUYENBAY";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                ChiTietChuyenBayPOJO ctcb = new ChiTietChuyenBayPOJO();
                ctcb.setMaChiTietChuyenBay(rs.getInt("MaChiTietChuyenBay"));
                ctcb.setMaChuyenBay(rs.getString("MaChuyenBay"));
                ctcb.setSanBayTrungGian(rs.getString("SanBayTrungGian"));
                ctcb.setThoiGianDung(rs.getInt("ThoiGianDung"));
                ctcb.setGhiChu(rs.getString("GhiChu"));
                ds.add(ctcb);
            }
            helper.close();
        } catch (SQLException ex) {
            Logger.getLogger(ChiTietChuyenBayDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }

    public static ArrayList<ChiTietChuyenBayPOJO> LayDanhSachChiTietCB(String maChuyenBay) {

        ArrayList<ChiTietChuyenBayPOJO> ds = new ArrayList<ChiTietChuyenBayPOJO>();
        try {
            String sql = "SELECT * FROM CHITIETCHUYENBAY WHERE MACHUYENBAY = '" + maChuyenBay + "'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                ChiTietChuyenBayPOJO ctcb = new ChiTietChuyenBayPOJO();
                ctcb.setMaChiTietChuyenBay(rs.getInt("MaChiTietChuyenBay"));
                ctcb.setMaChuyenBay(rs.getString("MaChuyenBay"));
                ctcb.setSanBayTrungGian(rs.getString("SanBayTrungGian"));
                ctcb.setThoiGianDung(rs.getInt("ThoiGianDung"));
                ctcb.setGhiChu(rs.getString("GhiChu"));
                ds.add(ctcb);
            }
            helper.close();
        } catch (SQLException ex) {
            Logger.getLogger(ChiTietChuyenBayDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ds;
    }

    public static boolean themChiTietCB(ChiTietChuyenBayPOJO ct) {
        boolean kq = false;
        String sql = String.format("INSERT INTO `CHITIETCHUYENBAY` (`MaChuyenBay`,`SanBayTrungGian`,`ThoiGianDung`,`GhiChu`) VALUES('%s','%s','%s','%s');", ct.getMaChuyenBay(), ct.getSanBayTrungGian(), ct.getThoiGianDung(), ct.getGhiChu());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;
        }
        helper.close();
        return kq;
    }

    public static boolean xoaChiTietCB(String maChuyenBay) {
        boolean kq = false;
        String sql = "DELETE FROM CHITIETCHUYENBAY WHERE MaChuyenBay = '" + maChuyenBay + "'";
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;
        }
        helper.close();
        return kq;
    }
}
